<template>
  <div class="task_form_dialog">
    <!-- 机构验证弹框 -->
    <el-dialog
      v-model="visible"
      title="验证"
      :append-to-body="true"
      :show-close="false"
      :close-on-click-modal="false"
      top="30vh"
      center
      width="450px"
    >
      <div v-loading="dialogLoading" class="dialog_container">
        <div class="form_box f_1">
          <el-form
            ref="formRef"
            :rules="rules"
            :model="form"
            label-width="125px"
            size="small"
          >
            <el-form-item label="机构名称：" prop="orgName">
              <span>{{ form.orgName }}</span>
            </el-form-item>

            <el-form-item label="验证的问题：" prop="verifyQuestion">
              <span>{{ form.verifyQuestion }}</span>
            </el-form-item>

            <el-form-item label="验证的答案：" prop="verifyAnswer">
              <el-input
                v-model.trim="form.verifyAnswer"
                placeholder="请输入验证的答案"
                maxlength="50"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <template #footer>
        <div class="dialog_footer">
          <el-button
            type="primary"
            @click="handleConfirm"
            :loading="confirmBtnLoading"
            size="small"
          >
            确定
          </el-button>
          <el-button @click="close" size="small">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const initFormData = () => {
  return {
    id: null,
    orgName: null,
    orgShortName: null,
    orgDesc: null,
    // 机构管理员id
    orgManagerId: null,
    // 机构管理员名称
    orgManagerName: null,
    // 是否可加入: 1 是，2 否
    canJoin: "1",
    isOpenVerification: "0",
    verifyQuestion: null,
    verifyAnswer: null,
  };
};

export default {
  name: "OrgVerifyDialog",
  setup(props, context) {
    const { proxy } = getCurrentInstance();
    const $api = proxy.$api;
    const router = useRouter();

    const isEdit = ref(false);
    // 弹框是否显示
    const visible = ref(false);
    // 是否开启整个弹框loading（开启时无法关闭弹框）
    const dialogLoading = ref(false);
    // 确认按钮loading（开启时可以关闭弹框）
    const confirmBtnLoading = ref(false);
    // 表单的引用
    const formRef = ref(null);
    // 表单数据
    const form = reactive(initFormData());
    // 表单校验规则
    const rules = reactive({
      // orgName: [{ required: true, message: "请输入机构名称", trigger: "blur" }],
      // verifyQuestion: [
      //   {
      //     required: true,
      //     trigger: "blur",
      //     validator: (rule, value, callback) => {
      //       if (!value && form.isOpenVerification === "1") {
      //         callback(new Error("请输入验证的问题"));
      //       } else {
      //         callback();
      //       }
      //     },
      //   },
      // ],
      verifyAnswer: [
        {
          required: true,
          trigger: "blur",
          validator: (rule, value, callback) => {
            if (!value && form.isOpenVerification === "1") {
              callback(new Error("请输入验证的答案"));
            } else {
              callback();
            }
          },
        },
      ],
    });

    const handleConfirm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          context.emit("verify", form);
        } else {
          return false;
        }
      });
    };

    // 打开弹框
    const open = (row) => {
      if (row) {
        Object.keys(form).forEach((item) => {
          form[item] = row[item];
        });
        isEdit.value = true;
      } else {
        isEdit.value = false;
      }
      visible.value = true;
    };

    // 关闭弹框
    const close = () => {
      if (confirmBtnLoading.value || dialogLoading.value) {
        return;
      }
      resetData();
      visible.value = false;
    };

    // 重置数据
    const resetData = () => {
      // 重置表单校验
      formRef.value && formRef.value.resetFields();
      // 重置表单数据
      Object.keys(form).forEach((item) => {
        form[item] = initFormData()[item];
      });
    };

    return {
      visible,
      dialogLoading,
      confirmBtnLoading,
      formRef,
      form,
      rules,
      open,
      close,
      handleConfirm,
      isEdit,
    };
  },
};
</script>
